package com.zzyl.serve.service.impl;

import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.DTO.NursingPlanDto;
import com.zzyl.serve.Vo.NursingPlanVo;
import com.zzyl.serve.Vo.NursingProjectPlanVo;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {

        //先查询主表
        NursingPlan byId = getById(id);

        //在通过主表查询子表
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery().eq(NursingProjectPlan::getPlanId, id)
                .list();

        List<NursingProjectPlanVo> collect = list.stream().map(Pp -> {
            NursingProjectPlanVo bean = BeanUtil.toBean(Pp, NursingProjectPlanVo.class);
            bean.setProjectId(String.valueOf(Pp.getProjectId()));
            bean.setExecuteCycle(String.valueOf(Pp.getExecuteCycle()));
            bean.setExecuteFrequency(Long.valueOf(Pp.getExecuteFrequency()));
            return bean;
        }).collect(Collectors.toList());

        NursingPlanVo bean = BeanUtil.toBean(byId, NursingPlanVo.class);

        bean.setProjectPlans(collect);

        return bean;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto Dto) {
        //先保存主表的信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(Dto, nursingPlan);
        this.save(nursingPlan);

        //批量加入护理项目表

        List<NursingProjectPlan> projectPlans = Dto.getProjectPlans();
        if(ObjUtil.isEmpty(projectPlans)){
            return 1;
        }
        projectPlans.forEach(pp-> pp.setPlanId(nursingPlan.getId()));


        boolean result = this.nursingProjectPlanService.saveBatch(projectPlans);

         return result ? 1:0  ;
        //批量加入中间表的信息



    }








    /**
     * 修改护理计划
     *
     * @param dto@return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //不管其他先删除子表
        Long id = dto.getId();

        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        nursingProjectPlanService.removeBatchByIds(list);

        //复制粘贴主表
        NursingPlan bean = BeanUtil.toBean(dto, NursingPlan.class);
        this.updateById(bean);

        //重新加子表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if(ObjUtil.isNotEmpty(projectPlans)){
            projectPlans.forEach(pp->{
                pp.setPlanId(bean.getId());
                nursingProjectPlanService.save(pp);
            });
        }


        return this.updateById(bean) ? 1:0;
    }

    /**
     * 删除护理计划
     *
     * @param id@return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long id) {

        //删除子表
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        nursingProjectPlanService.removeBatchByIds(list);

        //删除主表

        return this.removeById(id)? 1:0;
    }
}
